home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
NEW_TECH
/
NTUNZ2.ZIP
/
NT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-19
|
41KB
|
1,097 lines
/*---------------------------------------------------------------------------
nt.c
WinNT-specific routines for use with Info-ZIP's UnZip 5.1 and later.
1993-09-28 Henry Gessau <henryg@kullmar.kullmar.se>
Started hacking at this. Borrowed, pilfered and plundered code from OS/2
and MS-DOS versions and from ZIP, modifying as necessary.
Last updated: 19 Dec 1993
---------------------------------------------------------------------------*/
#include <windows.h>
#include "unzip.h"
#define MKDIR(path,mode) mkdir(path)
struct direct
{
char reserved [21];
char ff_attrib;
short ff_ftime;
short ff_fdate;
long size;
char d_name[MAX_PATH];
int d_first;
HANDLE d_hFindFile;
};
static int created_dir; /* used by mapname(), checkdir() */
static int renamed_fullpath; /* ditto */
static int fnlen; /* ditto */
static unsigned nLabelDrive; /* ditto */
/**********************/ /* Borrowed from ZIP 2.0 sources */
/* Function opendir() */ /* Difference: no special handling for */
/**********************/ /* hidden or system files. */
static struct direct *opendir(n)
const char *n; /* directory to open */
{
struct direct *d; /* malloc'd return value */
char *p; /* malloc'd temporary string */
WIN32_FIND_DATA fd;
/* Start searching for files in the MSDOS directory n */
if ( (d = (struct direct *)malloc(sizeof(struct direct))) == NULL ||
(p = malloc(strlen(n) + 5)) == NULL )
{
if ( d != NULL )
free((void *) d);
return NULL;
}
strcat(strcpy(p, n), "/*.*");
if ( INVALID_HANDLE_VALUE == (d->d_hFindFile = FindFirstFile(p, &fd)) )
{
free((voidp *)d);
free((voidp *)p);
return NULL;
}
strcpy(d->d_name, fd.cFileName);
free((voidp *)p);
d->d_first = 1;
return d;
} /* end of function opendir() */
/**********************/ /* Borrowed from ZIP 2.0 sources */
/* Function readdir() */ /* Difference: no special handling for */
/**********************/ /* hidden or system files. */
static struct direct *readdir(d)
struct direct *d; /* directory stream from which to read */
{
/* Return pointer to first or next directory entry, or NULL if end. */
if ( d->d_first )
d->d_first = 0;
else
{
WIN32_FIND_DATA fd;
if ( !FindNextFile(d->d_hFindFile, &fd) )
return NULL;
strcpy(d->d_name, fd.cFileName);
}
return (struct direct *)d;
} /* end of function readdir() */
/***********************/
/* Function closedir() */ /* Borrowed from ZIP 2.0 sources */
/***********************/
static void closedir(d)
struct direct *d; /* directory stream to close */
{
FindClose(d->d_hFindFile);
free(d);
}
/**********************/
/* Function mapattr() */
/**********************/
/* Identical to MS-DOS, OS/2 versions. */
/* However, NT has a lot of extra permission stuff, so this function should */
/* probably be extended in the future. */
int mapattr()
{
/* set archive bit (file is not backed up): */
pInfo->file_attr = (unsigned)(crec.external_file_attributes | 32) & 0xff;
return 0;
} /* end function mapattr() */
/****************************/ /* Get the file time in a format that */
/* Function getNTfiletime() */ /* can be used by SetFileTime() in NT */
/****************************/
int getNTfiletime(FILETIME *ft)
{
FILETIME lft; /* 64-bit value made up of two 32 bit [low & high] */
WORD wDOSDate; /* for converting from DOS date to Windows NT */
WORD wDOSTime;
/* Copy and/or convert time and date variables, if necessary; */
/* then set the file time/date. */
wDOSTime = (WORD)lrec.last_mod_file_time;
wDOSDate = (WORD)lrec.last_mod_file_date;
/* The DosDateTimeToFileTime() function converts a DOS date/time */
/* into a 64 bit Windows NT file time */
if (!DosDateTimeToFileTime(wDOSDate, wDOSTime, &lft))
{
printf("DosDateTime failed: %d\n", GetLastError());
return FALSE;
}
if (!LocalFileTimeToFileTime( &lft, ft))
{
printf("LocalFileTime failed: %d\n", GetLastError());
*ft = lft;
}
return TRUE;
}
/****************************/
/* Function close_outfile() */
/****************************/
void close_outfile()
{
FILETIME ft; /* File time type defined in NT */
HANDLE hFile; /* File handle defined in NT */
int gotTime;
/* don't set the time stamp on standard output */
if (cflag) {
fclose(outfile);
return;
}
gotTime = getNTfiletime(&ft);
/* Close the file and then re-open it using the Win32
* CreateFile call, so that the file can be created
* with GENERIC_WRITE access, otherwise the SetFileTime
* call will fail. */
fclose(outfile);
hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if ( hFile == INVALID_HANDLE_VALUE ) {
fprintf(stderr, "\nCreateFile error %d when trying set file time\n",
GetLastError());
}
else {
if (gotTime)
if (!SetFileTime(hFile, NULL, NULL, &ft))
printf("\nSetFileTime failed: %d\n", GetLastError());
CloseHandle(hFile);
}
/* HG: I think this could be done in the CreateFile call above - just */
/* replace 'FILE_ATTRIBUTE_NORMAL' with 'pInfo->file_attr & 0x7F' */
if (!SetFileAttributes(filename, pInfo->file_attr & 0x7F))
fprintf(stderr, "\nwarning (%d): could not set file attributes\n",
GetLastError());
return;
} /* end function close_outfile() */
/* ============================================================================
*/
/***********************/
/* Function isfloppy() */ /* more precisely, is it removable? */
/***********************/
static int isfloppy(int nDrive) /* 1 == A:, 2 == B:, etc. */
{
char rootPathName[4];
rootPathName[0] = 'A' + nDrive - 1; /* Build the root path name, */
rootPathName[1] = ':'; /* e.g. "A:/" */
rootPathName[2] = '/';
rootPathName[3] = '\0';
if ( GetDriveType(rootPathName) == DRIVE_REMOVABLE )
return TRUE;
else
return FALSE;
} /* end function isfloppy() */
/**************************/
/* Function IsVolumeFAT() */
/**************************/
static int IsVolumeFAT(char *name)
{
char *tmp0;
char rootPathName[4];
char tmp1[MAX_PATH], tmp2[MAX_PATH];
unsigned volSerNo, maxCompLen, fileSysFlags;
if (isalpha(name[0]) && (name[1] == ':'))
tmp0 = name;
else
{
GetFullPathName(name, MAX_PATH, tmp1, &tmp0);
tmp0 = &tmp1[0];
}
strncpy(rootPathName, tmp0, 3); /* Build the root path name, */
rootPathName[3] = '\0'; /* e.g. "A:/" */
GetVolumeInformation(rootPathName, tmp1, MAX_PATH, &volSerNo,
&maxCompLen, &fileSysFlags, tmp2, MAX_PATH);
if ( !strncmp(strupr(tmp2), "FAT", 3) )
return TRUE;
else
return FALSE;
}
/******************************/
/* Function IsFileNameValid() */
/******************************/
static int IsFileNameValid(char *name)
{
HFILE hf;
OFSTRUCT of;
hf = OpenFile(name, &of, OF_READ | OF_SHARE_DENY_NONE);
if (hf == HFILE_ERROR)
switch (GetLastError())
{
case ERROR_INVALID_NAME:
case ERROR_FILENAME_EXCED_RANGE:
return FALSE;
default:
return TRUE;
}
else
_lclose(hf);
return TRUE;
}
/**********************/
/* Function map2fat() */ /* Identical to OS/2 version */
/**********************/